'use strict';

let express = require('express');
let app = express();
let cors = require('cors');
let path = require('path');
let cookieParser = require('cookie-parser');
let morgan = require('morgan');

let bodyParser = require('body-parser');

let { verifyToken } = require('./utils/middleware.js');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

app.use(cors());

// 请求日志
app.use(morgan('tiny'));

// 检查 token 有效性
app.use('/api', verifyToken);

// 路由模块
let router = require('./router.js');

// 挂载路由中间件
app.use('/', router);

app.listen(process.env.PORT, () => {
  console.log(
    `letao Api ${process.env.BASE_TITLE}服务已启动, 请访问：http://${process.env.MYSQL_HOST}:${process.env.PORT}`
  );
});
